var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
    upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
}
//关闭自定义扩张控件弹出窗口
function fy_dialog_close() {
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
}
function getUpFields(name) {
    return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}
function getUpAccountFieldValue(name) {
    var field = getUpFields(name).eq(0);
    return {
        displayValue: field.is("span") ? field.text() : field.val(),
        value: eval("(" + field.parent("span").attr("fieldVal") + ")").value
    };
}

function addOrDelRepeat(name, newLength) {
    var fields = getUpFieldsByFieldval(name);
    var oldLength = fields.length;
    var tbody = getUpFieldsByFieldval(name).eq(0).parent();
    while (!tbody.is("tbody")) {
        tbody = tbody.parent();
    }
    for (var i = 0, length = Math.abs(newLength - oldLength); i < length; i++) {
        if (newLength > oldLength) {
            upWindow.fy_addEmpty(tbody.children().last());
        } else {
            upWindow.fy_delCurrentRepeat(tbody.children().last());
        }
    }
}

function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "checkbox":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "radio":
            if (editTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            }
            break;
        case "select":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + " option:selected")
                        .attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).attr("val4cal");
            }
            break;
        case "date":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
                if (obj.value != "") {
                    obj.value = obj.value + " 00:00";
                }
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "datetime":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "flowdealoption":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "lable":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relationform":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relation":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "project":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + "_txt").val();
                obj.id = field.find("#" + fieldVal.name).val().split('|')[1];
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).text();
            }
            break;
        case "attachment":
        case "image":
        case "document":
            if (editTag || browseTag) {
                var attNames = "";
                var tempField = field.find("#" + fieldVal.name);
                var tempFileId = "";
                if (tempField[0].tagName.toLowerCase() == "input") {
                    tempFileId = tempField.val();
                } else if (tempField[0].tagName.toLowerCase() == "span") {
                    tempFileId = tempField[0].innerHTML;
                }
                var atts = upWindow.getAttBySubreference(tempFileId);
                for (var j = 0; j < atts.length; j++) {
                    attNames += atts[j].filename + ",";
                }
                if (attNames != "") {
                    attNames = attNames.substr(0, attNames.length - 1);
                }
                obj.value = attNames;
            }
            break;
        case "outwrite":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "externalwrite-ahead":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "exchangetask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "querytask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}
function setUpFieldValue(name, value) {
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input").val(
            value);
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
            .trigger("blur");
}

function setUpFieldValue2(field, value, displayValue, index) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var addTag = field.hasClass(addClass);
    value = trim(value);
    displayValue = trim(displayValue);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }

    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":
            if (editTag) {
                var inField = field.find("#" + fieldVal.name);
                inField.val(value);
                inField.trigger("blur");
            } else if (addTag) {
                var inField = field.find("#" + fieldVal.name);
                inField.val(value);
                inField.trigger("blur");
            }
            break;
        case "checkbox":
            if (editTag) {
                field.find("#" + fieldVal.name).attr("checked", true);
                field.find("#" + fieldVal.name).trigger("click");
            }
            break;
        case "radio":
            if (editTag) {
                field.find(":radio").eq(value).attr("checked", true);
                field.find(":radio").eq(value).trigger("click");
            }
            break;
        case "select":
            if (editTag) {
                var options = field.find("#" + fieldVal.name + " option");
                for (var k = 0; k < options.length; k++) {
                    if (options.eq(k).text() == value)
                        options.eq(k).attr("selected", true);
                }
                field.find("#" + fieldVal.name).trigger("change");
            }
            break;
        case "date":
            if (editTag) {
                field.find("#" + fieldVal.name).val(value);
                field.find("#" + fieldVal.name).trigger("blur");
            }
            break;
        case "datetime":
            if (editTag) {
                field.find("#" + fieldVal.name).val(value);
                field.find("#" + fieldVal.name).trigger("blur");
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                alert(editTag);
                field.find("#" + fieldVal.name + "_txt").val(value);
            }
            break;
    }
}

/**
 * 去除左右空格
 */
function trim(str) {
    return (str) ? (typeof str == "string" ? str.replace(/^\s*/, "").replace(
            /\s*$/, "") : str) : "";
}

window.onload = function () {
    var providerName = encodeURI(getUpFieldValue(getUpFieldsByFieldval("供应商名称")).value);

    document.getElementById("code").disabled = true;
    document.getElementById("button").disabled = true;
    var xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                var text = xmlhttp.responseText;

                if (text != "") {
                    var result = eval("(" + text + ")");
                    if (result.success&& result.code) {
                        setUpFieldValue2(getUpFieldsByFieldval("供应商编码_").eq(0), result.code);
                        fy_dialog_close();
                    }else{
						document.getElementById("code").value = getUpFieldValue(getUpFieldsByFieldval("供应商编码")).value;
						document.getElementById("code").focus();
					}
                }
            } else {
                alert("连接服务器异常");
            }
            document.getElementById("code").disabled = false;
            document.getElementById("button").disabled = false;
            document.getElementById("code").value = getUpFieldValue(getUpFieldsByFieldval("供应商编码")).value;
            document.getElementById("code").focus();
        }
    };
    xmlhttp.open("get",
            "../oa2uapController.do?method=checkProviderNameIsExist&providerName="
            + providerName + "&temp=" + Math.random(), true);
    xmlhttp.send();
};

function onButtonClick() {
    var value = document.getElementById("code").value.replace(/ /g, '').toUpperCase();
    document.getElementById("code").value = value;
    if (value === '') {
        alert("供应商编码不能为空。");
    } else {
        document.getElementById("code").disabled = true;
        document.getElementById("button").disabled = true;
        var xmlhttp = null;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    var text = xmlhttp.responseText;
                    if (text.indexOf("注意：ERP系统中已存在供应商") == 0) {
                        alert(text);
                        setUpFieldValue2(getUpFieldsByFieldval("供应商编码_").eq(0), document.getElementById("code").value);
                        fy_dialog_close();
                    } else if (text == 1) {
                        setUpFieldValue2(getUpFieldsByFieldval("供应商编码_").eq(0), document.getElementById("code").value);
                        fy_dialog_close();
                    } else if (text == 2) {
                        alert("供应商编码已存在");
                    } else if (text == 3) {
                        alert("用户还未登陆，或者已掉线");
                    } else if (text == 4) {
                        alert("供应商编码不能为空。");
                    } else {
                        alert("系统异常");
                    }

                } else {
                    alert("连接服务器异常");
                }
                document.getElementById("code").disabled = false;
                document.getElementById("button").disabled = false;
            }
        };
        xmlhttp.open("get",
                "../oa2uapController.do?method=checkProviderCodeIsExist&providerCode="
                + value + "&temp=" + Math.random(), true);
        xmlhttp.send();
    }
}
function onEnterKeyPress() {
    if (event.keyCode == 13) {
        onButtonClick();
    }
}